package cn.net.shuxing.yifu.common.service;


import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.CashChangeType;
import cn.net.shuxing.yifu.common.constant.DealType;
import cn.net.shuxing.yifu.common.dto.CashDealRecordDto;
import cn.net.shuxing.yifu.common.dto.DealRecordDto;
import cn.net.shuxing.yifu.common.mapper.UserCashHistoryMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserCashHistoryService extends BaseService<UserCashHistory, UserCashHistoryMapper> {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDealHistoryService userDealHistoryService;

    public List<UserCashHistory> getsByUserIdAndUserType(Integer userId,
                                                         Integer pageNum,
                                                         Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Integer userType = userService.getByPrimaryKeyWithoutComplete(userId).getType();
        CommonExample example = new CommonExample(UserCashHistory.class);
        example.equals("userId", userId);
        example.equals("userType", userType);
        List<UserCashHistory> userCashHistories = getsByExample(example);
        if (userCashHistories != null && !userCashHistories.isEmpty()) {
            for (UserCashHistory userCashHistory : userCashHistories) {
                Integer type = userCashHistory.getType();
                if (type == CashChangeType.CASH_HANG_BUY) {
                    userCashHistory.setTypeName("挂买");
                }
                if (type == CashChangeType.CASH_HANG_SALE) {
                    userCashHistory.setTypeName("挂卖");
                }
                if (type == CashChangeType.CASH_RECHAREGE) {
                    userCashHistory.setTypeName("充值");
                }
                if (type == CashChangeType.CASH_WITHDRAW) {
                    userCashHistory.setTypeName("提现");
                }
                if (type == CashChangeType.CASH_HANG_BUY_CANSEL) {
                    userCashHistory.setTypeName("取消挂买单退款");
                }
                if (type == CashChangeType.CASH_UNFROZEN_BUY_BACK) {
                    userCashHistory.setTypeName("用户解冻退款");
                }
                if (type == CashChangeType.CASH_BUY_COIN_GRANT) {
                    userCashHistory.setTypeName("股东购买配币");
                }
                if (type == CashChangeType.CASH_SHAREHOLDER_BONES) {
                    userCashHistory.setTypeName("股东分红");
                }

            }
        }
        return userCashHistories;
    }

    public List<DealRecordDto> getsByUserIdAndDealType(Integer userId, Integer dealType, Integer pageNum, Integer pageSize) {
        ArrayList<DealRecordDto> list = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        CommonExample example = new CommonExample(UserCashHistory.class);
        example.equals("userId", userId);
        example.equals("userType", user.getType());
        example.equals("type", dealType);
        List<UserCashHistory> userCashHistories = getsByExample(example);
        if (userCashHistories != null && !userCashHistories.isEmpty()) {
            for (UserCashHistory userCashHistory : userCashHistories) {
                Integer type = userCashHistory.getType();
                if (type == CashChangeType.CASH_HANG_BUY) {
                    userCashHistory.setTypeName("挂买");
                }
                if (type == CashChangeType.CASH_HANG_SALE) {
                    userCashHistory.setTypeName("挂卖");
                }
                if (type == CashChangeType.CASH_RECHAREGE) {
                    userCashHistory.setTypeName("充值");
                }
                if (type == CashChangeType.CASH_WITHDRAW) {
                    userCashHistory.setTypeName("提现");
                }
                if (type == CashChangeType.CASH_HANG_BUY_CANSEL) {
                    userCashHistory.setTypeName("取消挂买单退款");
                }
                if (type == CashChangeType.CASH_UNFROZEN_BUY_BACK) {
                    userCashHistory.setTypeName("用户解冻退款");
                }
                if (type == CashChangeType.CASH_BUY_COIN_GRANT) {
                    userCashHistory.setTypeName("股东购买配币");
                }
                if (type == CashChangeType.CASH_SHAREHOLDER_BONES) {
                    userCashHistory.setTypeName("股东分红");
                }
                DealRecordDto dealRecordDto = new DealRecordDto();
                dealRecordDto.setOrderKey(userCashHistory.getOrderKey());
                dealRecordDto.setDealTypeName(userCashHistory.getTypeName());
                dealRecordDto.setCreateTime(userCashHistory.getCreateTime());
                dealRecordDto.setDealAliveCoinCount(userCashHistory.getCount());
                list.add(dealRecordDto);
            }
        }
        return list;
    }

    public List<DealRecordDto> getsCashByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<DealRecordDto> list = new ArrayList<>();
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        CommonExample example = new CommonExample(UserCashHistory.class);
        example.equals("userId", userId);
        example.equals("userType", user.getType());
        example.setOrderByClause("id DESC");
        List<UserCashHistory> userCashHistories = getsByExample(example);
        if (userCashHistories != null && !userCashHistories.isEmpty()) {
            for (UserCashHistory userCashHistory : userCashHistories) {
                Integer type = userCashHistory.getType();
                if (type == CashChangeType.CASH_HANG_BUY) {
                    userCashHistory.setTypeName("挂买");
                }
                if (type == CashChangeType.CASH_HANG_SALE) {
                    userCashHistory.setTypeName("挂卖");
                }
                if (type == DealType.SYS_BUY_BACK) {
                    userCashHistory.setTypeName("系统回购");
                }
                if (type == CashChangeType.CASH_RECHAREGE) {
                    userCashHistory.setTypeName("充值");
                }
                if (type == CashChangeType.CASH_WITHDRAW) {
                    userCashHistory.setTypeName("提现");
                }
                if (type == CashChangeType.CASH_HANG_BUY_CANSEL) {
                    userCashHistory.setTypeName("取消挂买单退款");
                }
                if (type == CashChangeType.CASH_UNFROZEN_BUY_BACK) {
                    userCashHistory.setTypeName("用户解冻退款");
                }
                if (type == CashChangeType.CASH_BUY_COIN_GRANT) {
                    userCashHistory.setTypeName("股东购买配币");
                }
                if (type == CashChangeType.CASH_SHAREHOLDER_BONES) {
                    userCashHistory.setTypeName("股东分红");
                }
                DealRecordDto dto = new DealRecordDto();
                dto.setOrderKey(userCashHistory.getOrderKey());
                dto.setCreateTime(userCashHistory.getCreateTime());
                dto.setDealAliveCoinCount(userCashHistory.getCount());
                dto.setDealTypeName(userCashHistory.getTypeName());
                list.add(dto);
            }
        }
        return list;
    }

    public List<UserCashHistory> getsByUserIdCash(Integer userId, Integer pageNum, Integer pageSize) {
        CommonExample userExample = new CommonExample(User.class);
        userExample.equals("id", userId);
        User user = userService.getByExample(userExample);
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserCashHistory.class);
        example.equals("userId", userId);
        example.equals("userType", user.getType());
        example.setOrderByClause("id DESC");
        List<UserCashHistory> userCashHistories = getsByExample(example);
        for (UserCashHistory userCashHistory : userCashHistories) {
            String typeName = "";
            Integer type = userCashHistory.getType();
            if (type == DealType.SYS_BUY_BACK) {
                typeName = "系统回购";
            }
            if (type == CashChangeType.CASH_HANG_BUY) {
                typeName = "挂买";
            }
            if (type == CashChangeType.CASH_HANG_SALE) {
                typeName = "挂卖";
            }
            if (type == CashChangeType.CASH_RECHAREGE) {
                typeName = "充值";
            }
            if (type == CashChangeType.CASH_WITHDRAW) {
                typeName = "提现";
            }
            if (type == CashChangeType.CASH_HANG_BUY_CANSEL) {
                typeName = "取消挂买单退款";
            }
            if (type == CashChangeType.CASH_UNFROZEN_BUY_BACK) {
                typeName = "用户解冻退款";
            }
            if (type == CashChangeType.CASH_BUY_COIN_GRANT) {
                typeName = "股东购买配币";
            }
            if (type == CashChangeType.CASH_SHAREHOLDER_BONES) {
                typeName = "股东分红";
            }
            userCashHistory.setDealTypeName(typeName);
            userCashHistory.setDealCashCount(userCashHistory.getCount());
        }
        return userCashHistories;
    }

    public List<CashDealRecordDto> getsByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        CommonExample userExample = new CommonExample(User.class);
        userExample.equals("id", userId);
        User user = userService.getByExample(userExample);
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserCashHistory.class);
        example.equals("userId", userId);
        example.equals("userType", user.getType());
        example.setOrderByClause("id DESC");
        List<CashDealRecordDto> cashDealRecordDtos = new ArrayList<>();
        List<UserCashHistory> userCashHistories = getsByExample(example);
        for (UserCashHistory userCashHistory : userCashHistories) {
            CashDealRecordDto list = new CashDealRecordDto();
            list.setCount(userCashHistory.getCount());
            String typeName = "";
            Integer type = userCashHistory.getType();
            if (type == DealType.SYS_BUY_BACK) {
                typeName = "系统回购";
            }
            if (type == CashChangeType.CASH_HANG_BUY) {
                typeName = "挂买";
            }
            if (type == CashChangeType.CASH_HANG_SALE) {
                typeName = "挂卖";
            }
            if (type == CashChangeType.CASH_RECHAREGE) {
                typeName = "充值";
            }
            if (type == CashChangeType.CASH_WITHDRAW) {
                typeName = "提现";
            }
            if (type == CashChangeType.CASH_HANG_BUY_CANSEL) {
                typeName = "取消挂买单退款";
            }
            if (type == CashChangeType.CASH_UNFROZEN_BUY_BACK) {
                typeName = "用户解冻退款";
            }
            if (type == CashChangeType.CASH_BUY_COIN_GRANT) {
                typeName = "股东购买配币";
            }
            if (type == CashChangeType.CASH_SHAREHOLDER_BONES) {
                typeName = "股东分红";
            }
            list.setType(typeName);
            list.setCreateTime(userCashHistory.getCreateTime());
            list.setOrderKey(userCashHistory.getOrderKey());
            cashDealRecordDtos.add(list);
        }
        return cashDealRecordDtos;
    }

    //    CASH_RECHAREGE = 13, //充值
//        CASH_UNFROZEN_BUY_BACK = 16,//解冻扣款
//        CASH_BUY_COIN_GRANT = 17, //购买配币
    public List<CashDealRecordDto> getSysCashier(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<CashDealRecordDto> list = new ArrayList<>();
        List<UserCashHistory> userCashHistories = mapper.getSysCashier();
        if (userCashHistories != null && !userCashHistories.isEmpty()) {
            for (UserCashHistory userCashHistory : userCashHistories) {
                CashDealRecordDto dto = new CashDealRecordDto();
                dto.setCount(userCashHistory.getCount());
                String type = "";
                if (userCashHistory.getType() == 13)
                    type = "充值";

                if (userCashHistory.getType() == 16)
                    type = "解冻扣款";

                if (userCashHistory.getType() == 17)
                    type = "购买配币";
                dto.setType(type);
                dto.setCreateTime(userCashHistory.getCreateTime());
                dto.setOrderKey(userCashHistory.getOrderKey());
                list.add(dto);
            }
        }
        return list;
    }

    //    CASH_SHAREHOLDER_BONES = 18;//分红
//    CASH_WITHDRAW = 14,  //提现
//    系统回购
    public List<CashDealRecordDto> getSysPay(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<CashDealRecordDto> list = new ArrayList<>();
        List<UserCashHistory> userCashHistories = mapper.getSysPay();
        if (userCashHistories != null && !userCashHistories.isEmpty()) {
            for (UserCashHistory userCashHistory : userCashHistories) {
                CashDealRecordDto dto = new CashDealRecordDto();
                dto.setCount(userCashHistory.getCount());
                String type = "";
                if (userCashHistory.getType() == 14)
                    type = "提现";
                if (userCashHistory.getType() == 18)
                    type = "分红";
                dto.setType(type);
                dto.setCreateTime(userCashHistory.getCreateTime());
                dto.setOrderKey(userCashHistory.getOrderKey());
                list.add(dto);
            }
        }
        List<UserDealHistory> userDealHistories = userDealHistoryService.getsByDealType(DealType.SYS_BUY_BACK);
        if (userDealHistories != null & !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                CashDealRecordDto dto = new CashDealRecordDto();
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                BigDecimal singlePrice = userDealHistory.getSinglePrice();
                BigDecimal cashCount = dealAliveCoinCount.multiply(singlePrice);
                dto.setCount(cashCount);
                dto.setOrderKey(userDealHistory.getOrderKey());
                dto.setCreateTime(userDealHistory.getCreateTime());
                dto.setType("系统回购");
                list.add(dto);
            }
        }
        return list;
    }
}
